package cn.ss.exam2302.web;

import cn.ss.exam2302.entity.JsonResult;
import cn.ss.exam2302.entity.User;
import cn.ss.exam2302.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public JsonResult login(String usrAccount, String usrPassword){
        User user = userService.queryOneByAccount(usrAccount);
        if (user.getUsrDelete() != null) {
            JsonResult.JSONRESULT_FAIL.setMessage("账号已被锁定，请联系系统管理员解锁账号！");
            return JsonResult.JSONRESULT_FAIL;
        }
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(usrAccount, usrPassword));
            return JsonResult.JSONRESULT_SUCCESS;
        } catch (UnknownAccountException e) {
            JsonResult.JSONRESULT_FAIL.setMessage("账号不存在！");
            return JsonResult.JSONRESULT_FAIL;
        } catch (AuthenticationException e) {
            JsonResult.JSONRESULT_FAIL.setMessage("密码错误！");
            return JsonResult.JSONRESULT_FAIL;
        }
    }

    @PostMapping("/checkAccount")
    @ResponseBody
    public JsonResult checkAccount(String usrAccount){
        System.out.println(usrAccount);
        User user = userService.queryOneByAccount(usrAccount);
        if (user == null) return JsonResult.JSONRESULT_SUCCESS;
        else return JsonResult.JSONRESULT_FAIL;
    }

    @GetMapping("/register")
    public String register(){
        return "register";
    }

    @PostMapping("/register")
    @ResponseBody
    public JsonResult register(User user){
        return userService.insert(user);
    }

    @GetMapping("/manageAccount")
    @RequiresRoles("SYSTEM")
    public String manageAccount(Model model){
        List<User> userList = userService.queryALl();
        model.addAttribute("userList", userList);
        return "manageAccount";
    }

    @PostMapping("/resetPassword")
    @RequiresRoles("SYSTEM")
    @ResponseBody
    public JsonResult resetPassword(String password, String usrAccount) {
        return userService.resetPassword(password, usrAccount);
    }

    @PostMapping("/delete")
    @RequiresRoles("SYSTEM")
    @ResponseBody
    public JsonResult delete(String status, String usrAccount) {
        return userService.delete(status, usrAccount);
    }
}
